package com.eastfair.home.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dao.ReportServiceAuditHistoryMapper;
import com.eastfair.home.dto.ReportServiceAuditHistoryDTO;
import com.eastfair.home.entity.ReportServiceAuditHistory;
import com.eastfair.home.service.ReportServiceAuditHistoryService;
import com.eastfair.home.vo.ReportServiceAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆服务审核记录历史
 * </p>
 *
 * @author linan
 * @date 2022-08-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportServiceAuditHistoryServiceImpl extends SuperServiceImpl<ReportServiceAuditHistoryMapper, ReportServiceAuditHistory> implements ReportServiceAuditHistoryService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportServiceAuditHistory> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportServiceAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ReportServiceAuditHistory> list(ReportServiceAuditHistoryDTO modelDTO, List<Long> ids) {
        return ((ReportServiceAuditHistoryService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'reportServiceAuditId', #args[0]?.reportServiceAuditId}," +
                            "{'reportSpaceAuditId', #args[0]?.reportSpaceAuditId}," +
                            "{'reportSpaceId', #args[0]?.reportSpaceId}," +
                            "{'exhibitionManageId', #args[0]?.exhibitionManageId}," +
                            "{'serviceItemId', #args[0]?.serviceItemId}," +
                            "{'serviceName', #args[0]?.serviceName}," +
                            "{'picUrl', #args[0]?.picUrl}," +
                            "{'calUnit', #args[0]?.calUnit}," +
                            "{'unitPrice', #args[0]?.unitPrice}," +
                            "{'surcharge', #args[0]?.surcharge}," +
                            "{'serviceCategoryId', #args[0]?.serviceCategoryId}," +
                            "{'serviceSpecsId', #args[0]?.serviceSpecsId}," +
                            "{'minBookNum', #args[0]?.minBookNum}," +
                            "{'inventoryNum', #args[0]?.inventoryNum}," +
                            "{'bookingNum', #args[0]?.bookingNum}," +
                            "{'remark', #args[0]?.remark}," +
                            "{'auditState', #args[0]?.auditState}," +
                            "{'auditOpinion', #args[0]?.auditOpinion}," +
                            "{'generateOrder', #args[0]?.generateOrder}," +
                            "{'bookingTime', #args[0]?.bookingTime}," +
                            "{'declarationSystem', #args[0]?.declarationSystem}," +
                            "{'providerSystem', #args[0]?.providerSystem}," +
                            "{'discount', #args[0]?.discount}," +
                            "{'auditUserId', #args[0]?.auditUserId}," +
                            "{'auditUserName', #args[0]?.auditUserName}," +
                            "{'auditTime', #args[0]?.auditTime}," +
                            "{'businessId', #args[0]?.businessId}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ReportServiceAuditHistory> listOfCache(ReportServiceAuditHistoryDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<ReportServiceAuditHistory> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(modelDTO.getReportServiceAuditId() != null, ReportServiceAuditHistory::getReportServiceAuditId, modelDTO.getReportServiceAuditId());
            queryWrapper.lambda().eq(modelDTO.getReportSpaceAuditId() != null, ReportServiceAuditHistory::getReportSpaceAuditId, modelDTO.getReportSpaceAuditId());
            queryWrapper.lambda().eq(modelDTO.getReportSpaceId() != null, ReportServiceAuditHistory::getReportSpaceId, modelDTO.getReportSpaceId());
            queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null, ReportServiceAuditHistory::getExhibitionManageId, modelDTO.getExhibitionManageId());
            queryWrapper.lambda().eq(modelDTO.getServiceItemId() != null, ReportServiceAuditHistory::getServiceItemId, modelDTO.getServiceItemId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getServiceName()), ReportServiceAuditHistory::getServiceName, modelDTO.getServiceName());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPicUrl()), ReportServiceAuditHistory::getPicUrl, modelDTO.getPicUrl());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCalUnit()), ReportServiceAuditHistory::getCalUnit, modelDTO.getCalUnit());
            queryWrapper.lambda().eq(modelDTO.getUnitPrice() != null, ReportServiceAuditHistory::getUnitPrice, modelDTO.getUnitPrice());
            queryWrapper.lambda().eq(modelDTO.getSurcharge() != null, ReportServiceAuditHistory::getSurcharge, modelDTO.getSurcharge());
            queryWrapper.lambda().eq(modelDTO.getServiceCategoryId() != null, ReportServiceAuditHistory::getServiceCategoryId, modelDTO.getServiceCategoryId());
            queryWrapper.lambda().eq(modelDTO.getServiceSpecsId() != null, ReportServiceAuditHistory::getServiceSpecsId, modelDTO.getServiceSpecsId());
            queryWrapper.lambda().eq(modelDTO.getMinBookNum() != null, ReportServiceAuditHistory::getMinBookNum, modelDTO.getMinBookNum());
            queryWrapper.lambda().eq(modelDTO.getInventoryNum() != null, ReportServiceAuditHistory::getInventoryNum, modelDTO.getInventoryNum());
            queryWrapper.lambda().eq(modelDTO.getBookingNum() != null, ReportServiceAuditHistory::getBookingNum, modelDTO.getBookingNum());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getRemark()), ReportServiceAuditHistory::getRemark, modelDTO.getRemark());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getAuditState()), ReportServiceAuditHistory::getAuditState, modelDTO.getAuditState());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getAuditOpinion()), ReportServiceAuditHistory::getAuditOpinion, modelDTO.getAuditOpinion());
            queryWrapper.lambda().eq(modelDTO.getGenerateOrder() != null, ReportServiceAuditHistory::getGenerateOrder, modelDTO.getGenerateOrder());
            queryWrapper.lambda().eq(modelDTO.getBookingTime() != null, ReportServiceAuditHistory::getBookingTime, modelDTO.getBookingTime());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getDeclarationSystem()), ReportServiceAuditHistory::getDeclarationSystem, modelDTO.getDeclarationSystem());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getProviderSystem()), ReportServiceAuditHistory::getProviderSystem, modelDTO.getProviderSystem());
            queryWrapper.lambda().eq(modelDTO.getDiscount() != null, ReportServiceAuditHistory::getDiscount, modelDTO.getDiscount());
            queryWrapper.lambda().eq(modelDTO.getAuditUserId() != null, ReportServiceAuditHistory::getAuditUserId, modelDTO.getAuditUserId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getAuditUserName()), ReportServiceAuditHistory::getAuditUserName, modelDTO.getAuditUserName());
            queryWrapper.lambda().eq(modelDTO.getAuditTime() != null, ReportServiceAuditHistory::getAuditTime, modelDTO.getAuditTime());
            queryWrapper.lambda().eq(modelDTO.getBusinessId() != null, ReportServiceAuditHistory::getBusinessId, modelDTO.getBusinessId());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ReportServiceAuditHistory::getId, ids);
        }
        queryWrapper.lambda().eq(ReportServiceAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ReportServiceAuditHistory> modelList = list(queryWrapper);
        return modelList;
    }

    @Override
    public List<ReportServiceAuditHistoryVO> queryReportServiceAuditHistory(ReportServiceAuditHistoryDTO reportServiceAuditHistoryDTO) {
        LambdaQueryWrapper<ReportServiceAuditHistory> wrapper = new QueryWrapper<ReportServiceAuditHistory>().lambda()
                .eq(ReportServiceAuditHistory::getIsDeleted, BusinessConstant.NO)
                .eq(ReportServiceAuditHistory::getServiceItemId, reportServiceAuditHistoryDTO.getServiceItemId())
                .eq(ReportServiceAuditHistory::getReportSpaceId, reportServiceAuditHistoryDTO.getReportSpaceId())
                .eq(ReportServiceAuditHistory::getServiceCategoryId, reportServiceAuditHistoryDTO.getServiceCategoryId())
                .eq(ReportServiceAuditHistory::getServiceSpecsId, reportServiceAuditHistoryDTO.getServiceSpecsId())
                .orderByDesc(ReportServiceAuditHistory::getAuditTime);
        List<ReportServiceAuditHistory> list = list(wrapper);
        return ConvertUtil.convertList(list,ReportServiceAuditHistoryVO.class);
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ReportServiceAuditHistoryVO doToVo(ReportServiceAuditHistory model) {
        ReportServiceAuditHistoryVO modelVo = new ReportServiceAuditHistoryVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ReportServiceAuditHistoryVO> doToVos(List<ReportServiceAuditHistory> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ReportServiceAuditHistoryVO> result = new ArrayList<>();
        for (ReportServiceAuditHistory model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
